package com.ryan.mms.organization.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ryan.framework.dao.impl.BaseHibernateDaoSupport;
import com.ryan.framework.tag.pagination.Page;
import com.ryan.mms.organization.dao.OrganizationDao;
import com.ryan.mms.organization.model.Organization;
import com.ryan.utils.StringUtil;

@Repository
public class OrganizationDaoImpl extends BaseHibernateDaoSupport<Organization, Long> implements OrganizationDao<Organization,Long> {

	@Override
	public List<Organization> findByOrganization(Organization organization) {
		List<Organization> organizationList = this.find(generateCriterionList(organization));
		return organizationList;
	}

	@Override
	public List<Organization> findByOrganization(Page page,Organization organization) {
		List<Organization> organizationList = this.find(page,generateCriterionList(organization));
		return organizationList;
	}

	@Override
	public void deleteByOrganizationIds(List<Integer> pks) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteByOrganizationIds(String pks) {
		// TODO Auto-generated method stub
		
	}
	
	private List<Criterion> generateCriterionList(Organization organization){
		List<Criterion> criterionList=new ArrayList<Criterion>();
		if(!StringUtil.isEmpty(organization.getOrganizationCode())){
			criterionList.add(Restrictions.like("organizationCode", organization.getOrganizationCode()));
		}
		if(!StringUtil.isEmpty(organization.getOrganizationName())){
			criterionList.add(Restrictions.like("organizationName", organization.getOrganizationName()));
		}
		if(!StringUtil.isNull(organization.getOrganizationLevel())){
			criterionList.add(Restrictions.eq("organizationLevel", organization.getOrganizationLevel()));
		}
		if(!StringUtil.isNull(organization.getOrganizationId())){
			criterionList.add(Restrictions.eq("organizationId", organization.getOrganizationId()));
		}
		if(!StringUtil.isEmpty(organization.getOrganizationAddress())){
			criterionList.add(Restrictions.like("organizationAddress", organization.getOrganizationAddress()));
		}
		if(!StringUtil.isNull(organization.getOrganizationType())){
			criterionList.add(Restrictions.eq("organizationType", organization.getOrganizationType()));
		}
		return criterionList;
	}

}
